package coder.nav.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 邮件发送
 * @author Bosen 2024/9/20 11:44
 */
@Service
public class NavMailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.nickname}")
    private String nickname;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 邮箱格式验证
     * @param email 邮箱
     */
    public boolean checkEmail(String email){
        String check = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        return matcher.matches();
    }

    /**
     * html邮件
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     */
    public boolean sendHtmlMail(String to, String subject, String content) {
        // 获取MimeMessage对象
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message, true);
            // 邮件发送人
            messageHelper.setFrom(new InternetAddress(from, nickname, "UTF-8"));
            // 邮件接收人
            messageHelper.setTo(to);
            // 邮件主题
            messageHelper.setSubject(subject);
            // 邮件内容，html格式
            messageHelper.setText(content, true);
            // 发送
            mailSender.send(message);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 附件邮件
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     * @param fileSuf 文件后缀
     */
    public boolean sendFileMail(String to, String subject, String content, String fileSuf) {
        // 获取MimeMessage对象
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message, true);
            // 邮件发送人
            messageHelper.setFrom(new InternetAddress(from, nickname, "UTF-8"));
            // 邮件接收人
            messageHelper.setTo(to);
            // 邮件主题
            messageHelper.setSubject(subject);
            // 邮件内容，html格式
            messageHelper.setText("", true);
            // 附件
            File file = File.createTempFile("file", "." + fileSuf);
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(content);
            bw.close();
            messageHelper.addAttachment(subject + "." + fileSuf, file);
            // 发送
            mailSender.send(message);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

}
